<template>
	<!--pages/serviceChooseGood/serviceChooseGood.wxml-->
	<view class="container">
		<view class="top">
			<image class="top-icon" src="/static/images/serviceWarn.png"></image>
			<text class="top-txt">请先选择售后商品</text>
		</view>
		<view class="contaniner_con">
			<block v-if="carList.length != 0">
				<view class="car-list-box car-list-box-overflow">
					<view class="car-list-item" v-for="(item, index) in carList" :key="id" :style="item.boxStyle">
						<view class="car-list" :data-index="index">
							<view class="car-list-choose" @tap.stop="select" :data-index="index">
								<image v-if="item.checked==true" src="/static/images/choose-1.png"></image>
								<image v-else src="/static/images/choose-2.png"></image>
							</view>
							<view class="car-list-goods-img">
								<image :src="item.goods_img" lazy-load="true" :data-goodsid="item.goods_id" mode="aspectFit"></image>
							</view>
							<view class="car-list-right">
								<view class="car-list-goods-name" :data-goodsid="item.goods_id">{{item.goods_name}}</view>
								<view class="car-goods-sku" :data-index="index">
									<view class="car-goods-sku-detail-box">
										<view class="car-goods-sku-detail">{{item.sku_json_info.name?item.sku_json_info.name:''}}</view>
									</view>
								</view>
								<view class="car-list-price-box">
									<!-- 开启促销 -->
									<view class="car-list-price">
										<text>￥{{item.price}}</text>
									</view>
									<view class="choose-sku-change border">
										<text class="qh-rt-single qh-rt-a-zu4424 choose-sku-change-icon" @click.stop="reduceNum" :data-index="index"></text>
										<view class="choose-sku-num">
											<input min="1" type="digit" @input="inputNum" :data-index="index" :value="item.less_num" :data-id="item.id" placeholder="1" placeholder-class="choose-sku-p"></input>
										</view>
										<text class="qh-rt-single qh-rt-jia1 choose-sku-change-icon" @click.stop="addNum" :data-index="index"></text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
			<view v-else class="nogoods">暂无可售后商品</view>
		</view>
		<view class="btn_style flex-spe-cn">
			<view class="btn" @tap="commit">下一步</view>
		</view>
	</view>
</template>

<script>
	// pages/serviceChooseGood/serviceChooseGood.js
	const app = getApp().globalData;

	export default {
		data() {
			return {
				carList: [], //售后商品
				order_no: "",
			};
		},

		components: {},
		props: {},
		onLoad: function(options) {
			var order_no = options.order_no;
			this.setData({
				order_no
			});
		},

		 async onShow() {
			 await this.$onLaunched;
			var order_no = this.order_no;
			this.getList(order_no);
		},

		methods: {
			inputNum(e){
				var index = parseInt(e.currentTarget.dataset.index);
				let carList = this.carList;
				let num = e.detail.value;
				carList[index].less_num = num;
				this.setData({
					carList: carList
				});
			},
			// 获取订单数据
			getList(order_no) {
				var that = this;
				getApp().globalData.sendRequest({
					url: "/Order/getRefundGoods",
					type: "post",
					data: {
						order_no: order_no
					},
					success: function(res) {
						if (res.status == 1) {
							that.setData({
								carList: res.sell_list
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none"
							});
						}
					}
				});
			},

			//单选按钮
			select: function(e) {
				var that = this;
				var index = e.currentTarget.dataset.index;
				var carList = that.carList;
				var checked = carList[index].checked;

				if (carList[index].num > 0) {
					carList[index].checked = !checked;
					that.setData({
						carList: carList
					});
				}
			},

			reduceNum(e) {
				var that = this;
				var index = e.currentTarget.dataset.index;
				var carList = that.carList;

				if (carList[index].less_num == 1) {
					uni.showToast({
						title: "售后数量大于0",
						icon: 'none'
					});
				} else {
					carList[index].less_num -= 1;
					that.setData({
						carList: carList
					});
				}
			},

			addNum(e) {
				var that = this;
				var index = e.currentTarget.dataset.index;
				var carList = that.carList;

				if (carList[index].less_num >= carList[index].num - Number(carList[index].wait_refund_num)) {
					uni.showToast({
						title: "售后数量不能大于购买数量",
						icon: 'none'
					});
				} else {
					carList[index].less_num += 1;
					that.setData({
						carList: carList
					});
				}
			},

			/**
			 * commit 提交
			 */
			commit() {
				var that = this;
				var list = that.carList;
				var checkNum = 0;
				var postList = [];

				for (var i = 0; i < list.length; i++) {
					list[i].goods_count = list[i].less_num;

					if (list[i].checked) {
						postList.push(list[i]);
						checkNum++;
					}
				}

				if (checkNum > 0) {
					uni.navigateTo({
						url: '../serviceChooseStyle/serviceChooseStyle?order_no=' + that.order_no + '&list=' + JSON.stringify(postList)
					});
				} else {
					uni.showToast({
						title: '请选择商品',
						duration: 1500,
						icon: "none",
						mask: true
					});
				}
			}

		}
	};
</script>
<style>
	@import "./serviceChooseGood.css";
</style>
